//修改VPN
function Change_VPN() {
    for (let i=0;i<=5;i++){
        home();
        sleep(1000);
        Next_Page();
        sleep(1000);
        NB_Click();
        sleep(1000);
        if (Is_off()) {
            sleep(1000);
            if (Msg_alter()) {
                sleep(1000);
                if (Is_on()) {
                    home();
                    sleep(1000);
                    return true;
                }
            }
        }
    }
    return false;
}

function Next_Page(){
    var next_page = id("com.huang.autorunmarket:id/goRightView");
    var result = has(next_page);
    if (result) {
        click(next_page);
        logd("进入下一页");
    }
}

function NB_Click(){
    var nekoBox = text("NekoBox");
    var result = has(nekoBox);
    if (result) {
        click(nekoBox);
        logd("打开NekoBox");
    }else {
        toast("没有找到NekoBox");
    }
}

function Is_off(){  //判断VPN是否关闭
    var vpn_stop = desc("stop");
    var result1 = has(vpn_stop);
    sleep(1000);
    if(result1) {
        if (click(vpn_stop)) {
            logd("VPN为打开状态...单机关闭");
            return true;
        }else {
            toast("关闭VPN失败");
            return false;
        }
    }else {
        logd("VPN为关闭状态...继续运行");
        return true;
    }
}

function Is_on(){  //判断VPN是否开启
    var vpn_start = desc("Connect");
    var result1 = has(vpn_start);
    sleep(1000);
    if(result1) {
        if (click(vpn_start)) {
            logd("VPN为关闭状态...单击打开");
            sleep(1000);

            var connect = text("Connected, tap to check connection");
            var result2 = has(connect);
            if (result2) {
                click(connect);
                logd("点击确认VPN是否打开成功");
                sleep(1000);
                for (let i=0; i<5; i++){
                    var success = textMatch("Success: HTTP handshake took.*ms");
                    var result3 = has(success);
                    if (result3) {
                        logd("打开VPN成功");
                        sleep(500);
                        return true;
                    }else {
                        sleep(1500);
                    }
                }
                toast("打开VPN失败");
                return false;
            }
        }else {
            toast("打开VPN失败");
            return false;
        }
    }else {
        logd("VPN为打开状态...继续运行");
        return true;
    }
}

function Msg_alter() {
    var edit = desc("Edit");
    if (has(edit)) {
        click(edit);
        logd("点击修改信息按钮");
        sleep(1000);

        var msg_btn = text("Username (Optional)");
        var result1 = waitExistNode(msg_btn, 5000);
        if (result1) {
            click(msg_btn);
            logd("修改信息...");
            sleep(1000);

            var msg_data = id("android:id/edit");
            if (has(msg_data)) {
                var num = "";
                var country = Get_country();
                logd("获取国家:" + country);
                // inputText(msg_data, "5Hj9Ks2Cv9He-res-CA-sid-");
                inputText(msg_data, "5Hj9Ks2Cv9He-res-"+country+"-sid-");
                for (let i=0; i<8; i++){
                    var m = random(0, 9);
                    pasteText(msg_data, m);
                    num = num + m;
                }
                logd("输入:" + num);
                sleep(500);

                var finish = id("android:id/button1");
                if (has(finish)) {
                    click(finish);
                    logd("修改完成");
                    sleep(1000);

                    var apply = id("moe.nb4a:id/action_apply");
                    if (has(apply)) {
                        click(apply);
                        logd("√保存修改");
                        return true;
                    }else {
                        toast("保存失败...没有找到保存按钮");
                        return false;
                    }
                }else {
                    toast("修改信息失败");
                    return false;
                }
            }else {
                toast("输入信息失败");
                return false;
            }
        }else {
            var title = text("Profile config");
            if (has(title)) {
                toast("没有找到信息栏");
                return false;
            }else {
                kill_process();
                return false;
            }
        }
    }else {
        toast("点击修改信息按钮失败");
        return false;
    }
}

function kill_process(){
    recentApps();  //最近APP任务按键
    sleep(1000);

    var touch1 = MultiPoint.get()  //上滑，可关闭进程
        //按下
        .action(0).x(500).y(1300).pointer(1).delay(10)
        .next()
        //移动
        .action(2).x(500).y(700).pointer(1).delay(100)
        .next()
        //弹起
        .action(1).x(500).y(500).pointer(1).delay(10);
    var result = multiTouch(touch1, null, null, 200);
    if (result) {
        logd("关闭NekoBox进程");
    }else {
        toast("进程关闭失败");
    }
}

function Get_country(){
    try {
        var url = "http://192.168.8.111/tg/getcountrycode";
        var get_data = http.httpGetDefault(url, 1000*30);
        var code = JSON.parse(get_data.toString())['code'];
        if (code === 200) {
            return JSON.parse(get_data.toString())['data']['country_code'];
        }else {
            toast("请求超时");
        }
    }catch (e) {
        toast("服务器获取国家异常:"+e);
    }

}

// Change_VPN();